$(document).ready(function(){
		
	//QQ
	$('.qq_close').click(function(){
		var qq_user_mail = $('.qq_user_mail').val();
		$.post(
		'ajax/qq_status.php',
		{'qq_user_mail':qq_user_mail},
		function(data){
		
		}
		);
	
	});//end QQ
	
	//饮食提交
	$('#jquery_score_button').click(function(e){
		
		var eatWhat = $("#eatWhat").val();
		var eatHowMany = $("#eatHowMany").val();
		var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl png'></span><span class='tips_footer fl f16 png'></span></div>");
		if(eatWhat == "" || eatHowMany == "")
		{
			function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('请先填写食物信息');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
			return false;
		}
		
		if(eatHowMany >= 1500)
		{
			function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('你真牛,~确定木有输错吃的份量?');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
			return false;
		}
		
		var uId = $('.hiddenUserId').val();
		$.post('ajax/userLogin.php',{'uId':uId},function(data1){
			if(data1 == 'no')
			{
				function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('亲~请完善个人信息后,再计算哦~');
				$('.alertTip').fadeIn();
				}
				alertInput();
				setTimeout(function(){alertHide()},2000);
				function alertHide(){
					$('.alertTip').fadeOut();
					setTimeout(function(){$('.alertTip').remove()},500);
					}

				return false;		
			}
			else
			{ 
				$.post("ajax/item.php",{"eatWhat":eatWhat,"eatHowMany":eatHowMany},function(data){
				//alert(data);
		 		
				
				var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl png'></span><span class='tips_footer fl f16 png'></span></div>");
					function alertInput(){
					$('.content').append(alertTips);
					$('.alertTip .tips_footer').html('提交成功~');
					$('.alertTip').fadeIn();
					}
					alertInput();
					setTimeout(function(){alertHide()},1000);
					function alertHide(){
						$('.alertTip').fadeOut();
						setTimeout(function(){$('.alertTip').remove()},500);
						
					/* if($.browser.msie){
						location.reload();
						} */
 					} 
				$(".score_foodScore").html(data);
				$("#eatWhat").val('');
				$("#eatHowMany").val(''); 
				
				/* delect tr node */
				$('.score_delete').click(function(e){
					var index = $('.score_delete').index(this);
					var data_id = $('.hidden_data_id').eq(index).val();

					$.post("ajax/delete_data.php",{"data_id":data_id},function(data){	
						
					});//end post
					
					function alertInput(){
					$('.content').append(alertTips);
					$('.alertTip .tips_footer').html('删除成功~');
					$('.alertTip').fadeIn();
					}
					alertInput();
					setTimeout(function(){alertHide()},1000);
					function alertHide(){
						$('.alertTip').fadeOut();
						setTimeout(function(){$('.alertTip').remove()},500);
					} 
					
					$(this).parent().parent().remove();
					return false;
				})//end delete 
				
				});//end post
			}//end if else 
		}); 
		
		return false;
	});
	
	$('#score_sub').unbind('click');
	$('#score_sub').live('click',function(){
		var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
		if($('.score_foodTbody td').eq(1).text() == "" || $('.score_foodTbody td').eq(2).text() == "")
		{
			function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('你今日还没有饮食记录~');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
			return false;
		}else
		{
			
		function makeRandom(x,y){
			return parseInt(Math.random() * (x - y + 1) + y);
		}
		
		var screenHeight = $(window).height();
		var loadingBar = $('.scoreLoading_bar').width();
		var loadingTips = new Array("正在检索食品...","正在分析食品成分...","正在读取您的身体数据...","正在分析添加剂成分...","综合分析...","正在计算分数...","计算完成");
		var st1 = makeRandom(300,600);
		var st2 = makeRandom(100,400) + st1;
		var st3 = 300 + st2;
		var st4 = makeRandom(300,700) + st3;
		var st5 = makeRandom(300,500) + st4;
		var st6 = makeRandom(700,900) + st5;
		var st7 = 300 + st6;
		
		$('#scroeLoading').show().height(screenHeight);
		$('.scoreLoading_cover').css('width',0);
		$('html').css('overflow','hidden');
		$('.loadingCenter').css('top',screenHeight/2-80);
		$('.loadingTips').html(loadingTips[0]);
		$('.scoreLoading_cover').animate({'width':st2/st6*loadingBar},st1);		
		setTimeout(function(){$('.loadingTips').html(loadingTips[1]);$('.scoreLoading_cover').animate({'width':st3/st6*loadingBar},st2-st1);},st1);
		setTimeout(function(){$('.loadingTips').html(loadingTips[2]);$('.scoreLoading_cover').animate({'width':st4/st6*loadingBar},st3-st2);},st2);
		setTimeout(function(){$('.loadingTips').html(loadingTips[3]);$('.scoreLoading_cover').animate({'width':st5/st6*loadingBar},st4-st3);},st3);
		setTimeout(function(){$('.loadingTips').html(loadingTips[4]);$('.scoreLoading_cover').animate({'width':st6/st6*loadingBar},st5-st4);},st4);
		setTimeout(function(){$('.loadingTips').html(loadingTips[5]);$('.scoreLoading_cover').animate({'width':st6/st6*loadingBar},st6-st5);},st5);
		setTimeout(function(){$('.loadingTips').html(loadingTips[6]);$('.scoreLoading_cover').animate({'width':st6/st6*loadingBar},st7-st6);},st6);
		setTimeout(function(){$('#scroeLoading').hide();$('html').css('overflow','scroll');},st7);
			
		var data_status = "1";
		
		$.post("ajax/count.php",{"data_status":data_status},function(data){
		 
		 var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			/* function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('分数已经计算好了~');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}

			$(".score_advice").html(data); */
		
		setTimeout(function(){$(".score_advice").html(data);},st6);
			
		});
		}//end else
		}); 
		

	//关闭按钮
	$('.personal_close').click(function(e){
		var zy_ageVal = $('.zy_ageVal').val();
		var zy_cityVal = $('.zy_cityVal').val();
		var zy_weightVal = $('.zy_weightVal').val();
		
		if(zy_ageVal == "" || zy_ageVal>140 || zy_ageVal<0 ||　zy_weightVal == "" || zy_cityVal == "" || zy_weightVal>200 || zy_weightVal<0)
		{

				var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
				function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('先完善个人信息吧 =_=');
				$('.alertTip').fadeIn();
				}
				alertInput();
				setTimeout(function(){alertHide()},2000);
				function alertHide(){
					$('.alertTip').fadeOut();
					setTimeout(function(){$('.alertTip').remove()},500);
					}
			
			/* $('.personal_info').css('top','150px')
				$('.novice_login').fadeIn();
				$('.personal_info')
				.fadeIn()
				.animate({
					'top':200
				},{duration:'normal',queue:false}); */
			return false;
		}
	
	e.preventDefault();
	});
	
	
	
	
	//ajax查询
	$('#eatWhat').unbind('keyup');
	$('.ajax_foodName').css('width',$('.score_eatWhat').width());
	$('#eatWhat').keyup(function(e){
		var eatWhat = $("#eatWhat").val();
		if(e.keyCode == 38 || e.keyCode == 40){
			if(e.keyCode == 40){
				var ajax_li_len = $('.ajax_foodName li').length;
				var li_index = $('.ajax_foodName li').index($('.ajax_addBg'));
				if(li_index == -1){
					li_index = 0;
					$('.ajax_foodName li').eq(0).addClass('ajax_addBg');
				}else if(li_index == ajax_li_len-1){}else{
					$('.ajax_foodName li').eq(li_index).removeClass('ajax_addBg');
					$('.ajax_foodName li').eq(li_index + 1).addClass('ajax_addBg');
				}
				var key_foodName = $('.ajax_addBg').text();
				$('.score_eatWhat').val(key_foodName);
				$('.score_eatWhat').focus();
			}else{
				var li_index = $('.ajax_foodName li').index($('.ajax_addBg'));
				if(li_index == 0 || li_index == -1){}else{
					$('.ajax_foodName li').eq(li_index).removeClass('ajax_addBg');
					$('.ajax_foodName li').eq(li_index - 1).addClass('ajax_addBg');
					var key_foodName = $('.ajax_addBg').text();
					$('.score_eatWhat').val(key_foodName);
					$('.score_eatWhat').focus();
				}
			}
		}else{
			$.post("ajax/ajax_search.php",{"eatWhat":eatWhat},function(data){
				if(data){
					if(eatWhat){
						$('.ajax_foodName').slideDown();
						$(".ajax_foodName").html(data);
						/* ajax_foodName */
						$('.ajax_foodName li').unbind("click");
						$('.ajax_foodName li').hover(function(){
							$(this).addClass('ajax_addBg');
						},function(){
							$(this).removeClass('ajax_addBg');
						});
						$('.ajax_foodName li').click(function(){
							var foodName = $(this).text();
							$('.score_eatWhat').val(foodName);
							$('.ajax_foodName').slideUp();
						})
						$('.eatWhat').blur(function(){$('.ajax_foodName').slideUp();})
					}else{
						$('.ajax_foodName').hide();
					}
				}else
				{
					$('.ajax_foodName').slideDown();
					$(".ajax_foodName").html('<span style="color:red">没有该类食品~换一个试试~</span>');
				}
			});
		}
	});
	

	
	/* delect tr node */
	$('.score_delete').click(function(e){
		var index = $('.score_delete').index(this);
		var data_id = $('.hidden_data_id').eq(index).val();
		var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
		
		$.post("ajax/delete_data.php",{"data_id":data_id},function(data){
			function alertInput(){
				$('.content').append(alertTips);
				$('.alertTip .tips_footer').html('删除成功~');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
		});
		
		$(this).parent().parent().remove();
		return false;
	})
	
	//个人信息录入提示
	$('.personal_submit').click(function(){
		var zy_ageVal = $('.zy_ageVal').val();
		var zy_cityVal = $('.zy_cityVal').val();
		var zy_weightVal = $('.zy_weightVal').val();
		
		if(zy_ageVal == "")
		{
			var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			function alertInput(){
				$('.personal_info').append(alertTips);
				$('.alertTip .tips_footer').html('不介意告把年龄告诉我吧 =_=');
				$('.alertTip').addClass('info_check');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
			return false;
		}
		if(zy_ageVal>140 || zy_ageVal<0)
		{
			var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			function alertInput(){
				$('.personal_info').append(alertTips);
				$('.alertTip .tips_footer').html('哇噻！！这年龄...你是火星人吧~');
				$('.alertTip').addClass('info_check');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}

			return false;
		}
		
		if(zy_cityVal == "")
		{
			var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			function alertInput(){
				$('.personal_info').append(alertTips);
				$('.alertTip .tips_footer').html('你住哪个省呢...');
				$('.alertTip').addClass('info_check');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}

			return false;
		}
		
		if(zy_weightVal == "")
		{
			var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			function alertInput(){
				$('.personal_info').append(alertTips);
				$('.alertTip .tips_footer').html('只有我一人知道你的体重呢O(∩_∩)O~');
				$('.alertTip').addClass('info_check');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}
			return false;
		}
		if(zy_weightVal>200 || zy_weightVal<0)
		{
			var alertTips = $("<div class='alertTip tc none'><span class='tips_head fl'></span><span class='tips_footer fl f16'></span></div>");
			function alertInput(){
				$('.personal_info').append(alertTips);
				$('.alertTip .tips_footer').html('你是超人吧，让我开眼界了o(╯□╰)o');
				$('.alertTip').addClass('info_check');
				$('.alertTip').fadeIn();
			}
			alertInput();
			setTimeout(function(){alertHide()},1000);
			function alertHide(){
				$('.alertTip').fadeOut();
				setTimeout(function(){$('.alertTip').remove()},500);
			}

			return false;
		}
	});

	
	
	//注册页面验证
	//验证邮箱
	$("#email").focus(function()
	{
		var email = $("#email").val();
		//alert(email);
		if(email.length == 0)
		{
			$("#emailTips").html('&#12288;例如:abc@email.com');
			
			return false;
		}//end email isAtn if
		else
		{
			if(email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
			{
				$("#emailTips").html('');
				return true;
			}
			else
			{
				$("#emailTips").html('&#12288;例如:abc@email.com');
				
				return false;
			}
		}//end focus else	
	});//end email focus
	
	$("#email").blur(function()
	{
		var email = $("#email").val();
		
		if(email.length == 0)
		{
			$("#emailTips").html('&#12288;邮箱不能为空！');

			return false;
		}//end email isAtn if
		else
		{
			if(email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
			{
				$.post("ajax/emailCheck.php",{"email":email},function(data)
				{
					if (data == 0)//邮箱可以注册
					{
						$("#emailTips").html('');
						return true;
					}
					else//邮箱已经被注册
					{
						$("#emailTips").html('&#12288;邮箱已被使用！');
						
						return false;
					}
				});//end post
			}//end isEmail
			else
			{
				$("#emailTips").html('&#12288;请输入正确的邮箱地址！');
				
				return false;
			}//end isEmail else
		}//end email isAtn else
	});//end email blur

	//密码验证
	$("#user_pass").focus(function()
	{
	
		var user_pass = $("#user_pass").val();
		
		if(user_pass.length == 0)
		{
			$("#user_passTips").html('&#12288;字母,数字或符号,最短六位');
			
			return false;
		}//end user_pass isAtn if
		else
		{
			if(user_pass.length > 5)
			{
				$("#user_passTips").html('');
				return true;
			}
			else
			{
				$("#user_passTips").html('&#12288;字母,数字或符号,最短六位');
				
				return false;
			}
		}//end focus else
	});
	
	$("#user_pass").blur(function()
	{
	
		var user_pass = $("#user_pass").val();
		
		if(user_pass.length == 0)
		{
			$("#user_passTips").html('&#12288;密码不能为空！');
			
			return false;
		}//end user_pass isAtn if
		else
		{
			if(user_pass.length > 5)
			{
				$("#user_passTips").html('');
				return true;
			}
			else
			{
				$("#user_passTips").html('&#12288;密码不能少于6位！');
				return false;
			}
		}//end blur else
	});//end blur
	
	//用户名验证
	$("#nick").focus(function()
	{
	
		var nick = $("#nick").val();
		
		if(nick.length == 0)
		{
			$("#nickTips").html('&#12288;2-12位中文,字母,数字,下划线');
			
			return false;
		}//end nick isAtn if
		else
		{
			if(nick.length >2)
			{
				if(nick.length <13)
				{
					$("#nickTips").html('');
					return true;
				}
				else
				{
					$("#nickTips").html('&#12288;2-12位中文,字母,数字,下划线');
					
					return false;
				}
			}
			else
			{
				$("#nickTips").html('&#12288;2-12位中文,字母,数字,下划线');
				
				return false;
			}
		}//end focus else
	});//end focus
	
	
	$("#nick").blur(function()
	{
	
		var nick = $("#nick").val();
		
		if(nick.length == 0)
		{
			$("#nickTips").html('&#12288;昵称不能少于2位！');
			
			return false;
		}//end nick isAtn if
		else
		{
			if(nick.length > 1)
			{
				if(nick.length < 13)
				{
					$.post("ajax/nickCheck.php",{"nick":nick},function(data){
		 	
					if (data == 0)//用户名可以注册
					{
						$("#nickTips").html('');
						return true;
					}
					else//用户名已经被注册
					{
						$("#nickTips").html('&#12288;昵称已被使用！');
						
						return false;
					}
					});//end post
				}
				else
				{
					$("#nickTips").html('&#12288;昵称不能超过12位！');
					
					return false;
				}
			}
			else
			{
				$("#nickTips").html('&#12288;昵称不能少于2位！');
				
				return false;
			}
		}//end blur else
	});//end blur
	
	//注册提交验证
	$("#register_submit").click(function()
	{
		var email = $("#email").val();

		if(email.length == 0)
		{
			$("#emailTips").html('&#12288;邮箱不能为空！');
			
			return false;
		}//end email isAtn if
		else
		{
			if(email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
			{
				$.post("ajax/emailCheck.php",{"email":email},function(data)
				{
					if (data == 0)//邮箱可以注册
					{
						$("#emailTips").html('');
						return true;
					}
					else//邮箱已经被注册
					{
						$("#emailTips").html('&#12288;邮箱已被使用！');
						
						return false;
					}
				});//end post
			}//end isEmail
			else
			{
				$("#emailTips").html('&#12288;请输入正确的邮箱地址！');
				$("#email").focus();
				return false;
			}//end isEmail else
		}//end email isAtn else
		
		var user_pass = $("#user_pass").val();
		
		if(user_pass.length == 0)
		{
			$("#user_passTips").html('&#12288;密码不能为空！');
			
			return false;
		}//end user_pass isAtn if
		else
		{
			if(user_pass.length > 5)
			{
				$("#user_passTips").html('');
				return true;
			}
			else
			{
				$("#user_passTips").html('&#12288;密码不能少于6位！');
				$("#user_pass").focus();
				return false;
			}
		}//end blur else
		
		var nick = $("#nick").val();
	
		if(nick.length == 0)
		{
			$("#nickTips").html('&#12288;昵称不能少于2位！');
			
			return false;
		}//end nick isAtn if
		else
		{
			if(nick.length > 1)
			{
				if(nick.length < 13)
				{
					$.post("ajax/nickCheck.php",{"nick":nick},function(data){
		 	
					if (data == 0)//用户名可以注册
					{
						$("#nickTips").html('');
						return true;
					}
					else//用户名已经被注册
					{
						$("#nickTips").html('&#12288;昵称已被使用！');
						
						return false;
					}
					});//end post
				}
				else
				{
					$("#nickTips").html('&#12288;昵称不能超过12位！');
					
					return false;
				}
			}
			else
			{
				$("#nickTips").html('&#12288;昵称不能少于2位！');
				
				return false;
			}
		}//end blur else
	});
	
	
	//登陆页面验证
	//邮箱非空验证
	$("#login_email").focus(function()
	{
		$("#login_fail_tip").html('');

	});//end email focus
	
	$("#login_pass").focus(function()
	{
		$("#login_fail_tip").html('');

	});//end email focus
	
	
	$("#login_email").blur(function()
	{
		var login_email = $("#login_email").val();
		
		if(login_email.length == 0)
		{
			$("#login_email_tips").html('&#12288;邮箱不能为空！');
			return false;
		}//end email isAtn if
		else
		{
			if(login_email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
			{
				$("#login_email_tips").html('');
				return true;
			}//end isEmail
			else
			{
				$("#login_email_tips").html('&#12288;请输入正确的邮箱地址！');
				
				return false;
			}//end isEmail else
		}//end email isAtn else
	});//end email blur
	
	//登录提交验证
	$('#login_submit').click(function(){
		
		var login_email = $("#login_email").val();
		
		if(login_email.length == 0)
		{
			$("#login_email_tips").html('&#12288;邮箱不能为空！');
			$("#login_email").focus();
			return false;
		}//end email isAtn if
		else
		{
			if(login_email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
			{
				$("#login_email_tips").html('');
				
				//密码非空验证
				var login_pass = $("#login_pass").val();
				
				if(login_pass.length == 0)
				{
					$("#login_pass_tips").html('&#12288;密码不能为空！');
					$("#login_pass").focus();
					return false;
				}else
				{
					$("#login_pass_tips").html('');
					return true;
				}
			}//end isEmail
			else
			{
				$("#login_email_tips").html('&#12288;请输入正确的邮箱地址！');
				$("#login_email").focus();
				return false;
			}//end isEmail else
		}//end email 0 else
	});//end login submit

	//点击食品类型获得所有分类食品
	$('.jq_detailFood a').live('click',function(){
		$('.js_foodName').slideDown(100);
		var category = $(this).text();
		$.post('ajax/getFoodName.php',{'category':category},function(data){
			$('.jq_lastFood').html(data);
		})
		return false;
	})
	
	
	
	

	
	

});

